﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
//using IronPython.Hosting;

namespace ReporterEngine
{
    public abstract partial class Model<TNode>
    {
        //types
        //ids
        //refs
        //
        public readonly Dictionary<string, TNode> IDs = new Dictionary<string, TNode>();
        public readonly Dictionary<string, List<TNode>> REFs = new Dictionary<string, List<TNode>>();
        public readonly List<MetaModelType> ModelTypes = new List<MetaModelType>();

        public Dictionary<string,string> Parameters = new Dictionary<string, string>();


        public abstract void Parse(XDocument source);

        protected abstract string GetAttributeValue(string name, TNode node);

        public virtual MetaModelType CreateNodeType()
        {
            return new MetaModelType();
        }

        public void SetParameterValue(string parameterName, string value)
        {
            parameterName = parameterName.ToLower();

            if (Parameters.ContainsKey(parameterName))
                Parameters[parameterName] = value;
            else
                Parameters.Add(parameterName,value);       
        }

        public string GetParameterValue(string parameterName)
        {
            parameterName = parameterName.ToLower();

            if (Parameters.ContainsKey(parameterName))
                return Parameters[parameterName];
            
            return string.Empty;
        }

        //public virtual sQuery Eval(string path,object query)
        //{
        //    var en = Python.CreateEngine(AppDomain.CurrentDomain);
        //    var scope = en.CreateScope();

        //    scope.SetVariable("source", query);

        //    var res3 = en.Execute(path, scope);

        //    return res3 as sQuery;
        //}

        //public abstract sQuery Eval(string path);

        public static string[] NormalizeNamesList(string names)
        {
            var result = new List<string>();
            
            string[] typeList = names.Split(",".ToCharArray());

             foreach (var typeName in typeList)
             {
                 var clearName = typeName.Trim();

                 clearName = clearName.Replace("'", "");
                 clearName = clearName.Replace("”", "");
                 clearName = clearName.Replace("“", "");
                 clearName = clearName.Replace("'", "");
                 clearName = clearName.Replace("'", "");
                 clearName = clearName.Replace("'", "");

                 clearName = clearName.Replace("\"", "");

                 clearName = clearName
                     .Replace("\"", "")
                     .Replace("'", "")
                     .Replace("’", "")
                     .Replace("‘", "")
                     .Replace(",", "")
                     .Replace("”", "")
                     .Replace("“", "")
                     .Replace("„", "");

                 clearName = clearName.ToLower();

                 result.Add(clearName);
             }

             return result.ToArray();
        }

        public abstract string GetPropertyName(TNode node);
        public abstract string GetTypeName(TNode node);
       

        public class MetaModelType
        {
            public TNode Data { get; set; }
            public string Label { get; set; }
            public string PluralLabel { get; set; }

            public List<TNode> Nodes = new List<TNode>();
        }
            
    }
}
